﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.ComControls;
using MINERP.OldSamllPicPrint.View;
using MINERP.MODEL.SmallPic;
using MINERP.MODEL.FoundationModel;
using MINERP.OldSamllPicPrint.Presenter;
using MINERP.OldSamllPicPrint.Forms;
using MINERP.OldSamllPicPrint.Properties;
using System.Configuration;
using MINERP.COM;

namespace MINERP.OldSamllPicPrint
{
    public partial class FrmMain : FrmParent, IMainView
    {
        private int _curTechId = 0;
        private int _curUserId = 0;
        private int _printerId = 0;

        private MainPresenter _presenter;

        public FrmMain()
        {
            InitializeComponent();
            base.InitiAuthority();
            barStaticItem1.Caption = base.userName;
        }

        #region IMainView 成员

        public int CurTechId
        {
            get
            {
                return _curTechId;
            }
            set
            {
                _curTechId = value;
            }
        }

        public int OperUid
        {
            get
            {
                return _curUserId;
            }
            set
            {
                _curUserId = value;
            }
        }

        public int PrinterId
        {
            get
            {
                return _printerId;
            }
            set
            {
                _printerId = value;
            }
        }

        public void ShowTaskList(List<ProductView> _curTaskList)
        {
            this.gridControl1.DataSource = _curTaskList;
            this.gridControl1.RefreshDataSource();
            barListItem1.Caption = _curTaskList.Count.ToString();
        }

        #endregion

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //全选 
            _presenter.FullSelect();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //反选
            _presenter.ReSelect();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            _presenter.ExPortPic();//导出照片
            gridControl1.RefreshDataSource();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            _presenter.PrintSelectTask();//打印条码
        }

        private void simpleButton5_Click(object sender, EventArgs e)//完成选中的产品
        {
            Cursor = Cursors.WaitCursor;
            _presenter.CompleteSelectTask();
            Cursor = Cursors.Default;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //刷新
            this.Cursor = Cursors.WaitCursor;
            _presenter.IniFrmView();
            Cursor = Cursors.Default;
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmSelSavePicDir frm = new FrmSelSavePicDir();
            frm.SelectedDirFolder = DataTransfer.StrReportDir;
            if (DialogResult.OK == frm.ShowDialog())
            {
                DataTransfer.StrReportDir = frm.SelectedDirFolder;
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmMain.GetInstance(this.userId);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _curTechId = base.CurrentTechID;
            _curUserId = base.userId;
            _printerId = base.PrinterId;

            barStaticItem1.Caption = base.userName;
            //chkMoveByPhotoNum.Checked = Settings.Default.MoveByPhotoNum;
            try
            {
                chkMoveByPhotoNum.Checked =Convert.ToBoolean( ConfigurationManager.AppSettings.Get("MoveByPhotoNum"));
            }
            catch (Exception)
            {
                chkMoveByPhotoNum.Checked = true;
                throw;
            }

            DataTransfer.MoveByPhotoNum = chkMoveByPhotoNum.Checked;

            _presenter = new MainPresenter(this);

        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            //打印某产品的条码信息
            string strCode = txtProductCode.Text.Trim();
            if (string.IsNullOrEmpty(strCode))
            {
                MessageBox.Show("请输入产品编号！");
            }
            else
            {
                if (strCode.Length < 10)
                {
                    int OrderProductId = Convert.ToInt32(strCode);
                    strCode = "2" + string.Format("{0:d9}", OrderProductId);
                }

                int ret = _presenter.PrintOrderInfo(strCode);
           
            }
        }

        private void chkMoveByPhotoNum_CheckedChanged(object sender, EventArgs e)
        {
            ModifyConfigurationOP.SetConfig("ModifyConfigurationOP", chkMoveByPhotoNum.Checked?"True":"False");
            DataTransfer.MoveByPhotoNum = chkMoveByPhotoNum.Checked;
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle>-1)
            {
                if (((ProductView)gridView1.GetRow(e.RowHandle)).Status == 1)
                {
                    e.Appearance.BackColor = Color.GreenYellow;//已导过片
                }
            }
        }

        private void btnExportFolder_Click(object sender, EventArgs e)
        {
            _presenter.ExPortOrderFloderPic();//导出照片
            gridControl1.RefreshDataSource();
        }
    }
}
